package main

import "fmt"

func main() {
	shareSlice()
}

func shareSlice() {
	s1 := []int{1, 2, 3, 4}
	s2 := s1[2:]
	fmt.Printf("s1: %v, %d, %d \n", s1, len(s1), cap(s1))
	fmt.Printf("s2: %v, %d, %d \n", s2, len(s2), cap(s2))

	// s1和s2结构无变化，就是共享数据
	s2[0] = 99
	fmt.Printf("s1: %v, %d, %d \n", s1, len(s1), cap(s1))
	fmt.Printf("s2: %v, %d, %d \n", s2, len(s2), cap(s2))

	// append导致结构发生变化，s1和s2不在共享内存
	s2 = append(s2, 199)
	fmt.Printf("s1: %v, %d, %d \n", s1, len(s1), cap(s1))
	fmt.Printf("s2: %v, %d, %d \n", s2, len(s2), cap(s2))

	s2[1] = 1999
	fmt.Printf("s1: %v, %d, %d \n", s1, len(s1), cap(s1))
	fmt.Printf("s2: %v, %d, %d \n", s2, len(s2), cap(s2))
}
